package controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import pojo.User;
import service.Userservice;

@Controller
public class LoginController {
	@Resource
	private Userservice service;
	public void setService(Userservice service) {
		this.service = service;
	}
	/**跳转到登录界面*/
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String login() {
		return "login";
	}
	/**用户登录*/
	@RequestMapping(value = "/login",method = RequestMethod.POST)
	public String login(@RequestParam String name,@RequestParam String pwd,
			HttpServletRequest request,HttpSession session) {
		User user = service.Login(name,pwd);
		if(null!=user) {
			session.setAttribute("user", user);
			return "redirect:index";
		}else {
			request.setAttribute("msg", "用户名或密码不正确");
			return "login";
		}
	}
	
	/**退出登录*/
	@RequestMapping("/loginout")
	public String loginout(HttpServletRequest request,HttpSession session) {
		request.setAttribute("msg", "退出成功");
		session.invalidate();
		return "login";
	}

}
